﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using M7;

namespace C7
{
	/// <summary>
	/// Interaction logic for BaseWindow.xaml
	/// </summary>
    public partial class LoginWindow : UserControl
    {
        public static WindowOperations WindowOPs = new WindowOperations();
        public LoginWindow()
        {
            this.InitializeComponent();
            WindowOPs.TextBoxesToHandlers(LayoutRoot);
            //WindowOPs.WindowToAnimationHandlers(LayoutRoot, true);
            spClose.MouseLeftButtonDown += new MouseButtonEventHandler(spClose_MouseLeftButtonDown);
            this.Loaded += new RoutedEventHandler(LoginWindow_Loaded);
        }

        void LoginWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var taOpen = new ThicknessAnimation();
            var sb = new Storyboard();
            var panel = this.Parent as Panel;
            taOpen.From = new Thickness(panel.ActualWidth, 0, panel.ActualWidth, 0);
            taOpen.To = new Thickness(0);
            taOpen.Duration = new Duration(TimeSpan.FromSeconds(1));
            Storyboard.SetTargetName(taOpen, "LayoutRoot");
            Storyboard.SetTargetProperty(taOpen, new PropertyPath(Grid.MarginProperty));
            sb.Children.Add(taOpen);
            sb.Begin(this);
        }
        Storyboard sb = new Storyboard();
        public void spClose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        
        {
            var taClose = new ThicknessAnimation();
            sb = new Storyboard();
            sb.Completed += new EventHandler(sb_Completed);
            taClose.To = new Thickness(this.ActualWidth, 0, this.ActualWidth, 0); ;
            taClose.Duration = new Duration(TimeSpan.FromSeconds(1));
            Storyboard.SetTargetName(taClose, "LayoutRoot");
            Storyboard.SetTargetProperty(taClose, new PropertyPath(Grid.MarginProperty));
            sb.Children.Add(taClose);
            sb.Begin(this);
        }
        }

        void sb_Completed(object sender, EventArgs e)
        {
            if (this.Parent is Panel)
            {
                var panel = this.Parent as Panel;
                panel.Children.Remove(this);
            }
        }

        private void spOKMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NCustomer obj = new NCustomer();
            obj.Username = tbUserName.Text;
            obj.Password = tbPassword.Password;
            bool IsNewCustomer = true;
            foreach (var cust in Info.current.customers)
            {
                if (cust.Username == obj.Username)
                {
                    IsNewCustomer = false;
                    break;
                }
            }
            if (IsNewCustomer)
            {
                Thread thr = new Thread(delegate() { Customer.SignIn(ref obj); });
                thr.IsBackground = true;
                thr.Start();
            }
            else
            {
                MessageBox.Show("Customer Is Already Signed In");
            }
        }

        private void tbSignUp_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //mesh 3aref -_-
        }
    }
}